package cn.jinjimi.app.main.widget.Behavior;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import cn.jinjimi.app.R;

public class PageBehavior extends AppBarLayout.ScrollingViewBehavior {
    private static final String TAG = "PageBehavior";

    public PageBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    boolean showIntercept;

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        showIntercept = dy < 0;
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, View child, MotionEvent ev) {

        if (showIntercept) {
            parent.findViewById(R.id.lv_header).onTouchEvent(ev);
            return false;
        }

        return super.onInterceptTouchEvent(parent, child, ev);
    }
}